package com.lemon.service.impl;

import cn.hutool.core.date.DateUtil;
import com.lemon.service.AiService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class AiServiceImpl implements AiService {

    // ==================== 默认模型客户端 ====================
    private final ChatClient chatClient;

    // ==================== 角色专用客户端 ====================
    private final ChatClient customerServiceChatClient;
    private final ChatClient dataAnalystChatClient;

    // ==================== 阿里云Qwen模型客户端 ====================
    private final ChatClient qwenPlusChatClient;
    private final ChatClient qwenTurboChatClient;

    // ==================== OpenAI模型客户端 ====================
    private final ChatClient gpt35ChatClient;
    private final ChatClient gpt4ChatClient;


    // 手动构造函数注入所有带@Qualifier注解的Bean
    public AiServiceImpl(
            ChatClient chatClient,
            @Qualifier("customerServiceChatClient") ChatClient customerServiceChatClient,
            @Qualifier("dataAnalystChatClient") ChatClient dataAnalystChatClient,
            @Qualifier("qwenPlusChatClient") ChatClient qwenPlusChatClient,
            @Qualifier("qwenTurboChatClient") ChatClient qwenTurboChatClient,
            @Qualifier("gpt35ChatClient") ChatClient gpt35ChatClient,
            @Qualifier("gpt4ChatClient") ChatClient gpt4ChatClient) {
        this.chatClient = chatClient;
        this.customerServiceChatClient = customerServiceChatClient;
        this.dataAnalystChatClient = dataAnalystChatClient;
        this.qwenPlusChatClient = qwenPlusChatClient;
        this.qwenTurboChatClient = qwenTurboChatClient;
        this.gpt35ChatClient = gpt35ChatClient;
        this.gpt4ChatClient = gpt4ChatClient;
    }

    @Override
    public String chat(String question, String sessionId) {

        return chatClient.prompt()
                .system(prompt -> prompt.param("now", DateUtil.now()))
                .advisors(advisorSpec ->
                        advisorSpec
                                .param(AbstractChatMemoryAdvisor
                                        .CHAT_MEMORY_CONVERSATION_ID_KEY, sessionId))
                .user(question)
                .call()
                .content();
    }

    @Override
    public String chatWithRoleAndModel(String question, String sessionId, String role, String model) {
        ChatClient selectedClient = selectChatClientByRoleAndModel(role, model);

        return selectedClient.prompt()
                .system(prompt -> prompt.param("now", DateUtil.now()))
                .advisors(advisorSpec ->
                        advisorSpec
                                .param(AbstractChatMemoryAdvisor
                                        .CHAT_MEMORY_CONVERSATION_ID_KEY, sessionId))
                .user(question)
                .call()
                .content();
    }

    private ChatClient selectChatClientByRoleAndModel(String role, String model) {
        // 首先根据模型选择客户端
        ChatClient modelClient = selectChatClientByModel(model);

        // 然后根据角色选择客户端
        switch (role) {
            case "customer_service":
                return customerServiceChatClient;
            case "data_analyst":
                return dataAnalystChatClient;
            default:
                // 如果没有指定特殊角色，则使用基于模型选择的客户端
                return modelClient;
        }
    }

    private ChatClient selectChatClientByModel(String model) {
        // ==================== 阿里云Qwen模型 ====================
        if (model.startsWith("qwen")) {
            switch (model) {
                case "qwen-turbo":
                    return qwenTurboChatClient; // 使用阿里云Qwen-Turbo模型
                case "qwen-plus":
                default:
                    return qwenPlusChatClient; // 使用阿里云Qwen-Plus模型
            }
        }
        // ==================== OpenAI模型 ====================
        else if (model.startsWith("gpt")) {
            switch (model) {
                case "gpt-4":
                    return gpt4ChatClient; // 使用OpenAI GPT-4模型
                case "gpt-3.5-turbo":
                default:
                    return gpt35ChatClient; // 使用OpenAI GPT-3.5-Turbo模型
            }
        }
        // ==================== 默认模型 ====================
        else {
            return chatClient; // 使用默认模型
        }
    }
}
